/*
 * MainWindow.java
 *
 * Created on February 19, 2008, 6:38 PM
 */

package projectprophet.app;

import projectprophet.core.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

/**
 *
 * @author  luppy
 */
public class MainWindow extends javax.swing.JFrame
{
	
	/** Creates new form MainWindow */
	public MainWindow()
	{
		initComponents();
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jTabbedPane1 = new javax.swing.JTabbedPane();
        developerTab = new javax.swing.JScrollPane();
        developerTable = new javax.swing.JTable();
        developersTaskViewTab = new javax.swing.JScrollPane();
        developerTaskWidget1 = new projectprophet.app.DeveloperTaskWidget();
        jScrollPane1 = new javax.swing.JScrollPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newProjectMenuItem = new javax.swing.JMenuItem();
        openProjectMenuItem = new javax.swing.JMenuItem();
        saveProjectMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        quitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Project Prophet");
        setName("mainWindow");
        developerTable.setModel(new DeveloperTableModel(Main.getProject()));
        developerTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        developerTab.setViewportView(developerTable);

        jTabbedPane1.addTab("Developers", developerTab);

        javax.swing.GroupLayout developerTaskWidget1Layout = new javax.swing.GroupLayout(developerTaskWidget1);
        developerTaskWidget1.setLayout(developerTaskWidget1Layout);
        developerTaskWidget1Layout.setHorizontalGroup(
            developerTaskWidget1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 454, Short.MAX_VALUE)
        );
        developerTaskWidget1Layout.setVerticalGroup(
            developerTaskWidget1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 386, Short.MAX_VALUE)
        );
        developersTaskViewTab.setViewportView(developerTaskWidget1);

        jTabbedPane1.addTab("Developer's task view", developersTaskViewTab);

        jTabbedPane1.addTab("tab2", jScrollPane1);

        fileMenu.setText("File");
        newProjectMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newProjectMenuItem.setText("New Project");
        newProjectMenuItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                newProject(evt);
            }
        });

        fileMenu.add(newProjectMenuItem);

        openProjectMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openProjectMenuItem.setText("Open Project");
        fileMenu.add(openProjectMenuItem);

        saveProjectMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveProjectMenuItem.setText("Save Project");
        saveProjectMenuItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveProject(evt);
            }
        });

        fileMenu.add(saveProjectMenuItem);

        saveAsMenuItem.setText("Save Project As");
        saveAsMenuItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveProjectAs(evt);
            }
        });

        fileMenu.add(saveAsMenuItem);

        fileMenu.add(jSeparator1);

        quitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        quitMenuItem.setText("Quit");
        quitMenuItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                quit(evt);
            }
        });

        fileMenu.add(quitMenuItem);

        jMenuBar1.add(fileMenu);

        helpMenu.setText("Help");
        jMenuItem5.setText("About");
        helpMenu.add(jMenuItem5);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
	
	private void saveProjectAs(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveProjectAs
	{//GEN-HEADEREND:event_saveProjectAs
		String projectFilename = getProjectFilename();
		if( projectFilename != null )
		{
			Project project = Main.getProject();
			String oldProjectfileName = project.getFileName();
			project.setFileName(projectFilename);
			if( !saveProject() )
			{
				project.setFileName(oldProjectfileName);
			}
		}
	}//GEN-LAST:event_saveProjectAs
	
	private void saveProject(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveProject
	{//GEN-HEADEREND:event_saveProject
		Project project = Main.getProject();
		String projectFileName = project.getFileName();
		if( projectFileName == null )
		{
			projectFileName = getProjectFilename();
			project.setFileName(projectFileName);
		}
		if( projectFileName != null )
		{
			saveProject();
		}
	}//GEN-LAST:event_saveProject
	
	private String getProjectFilename()
	{
		JFileChooser chooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Project Prophet Files", "project");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION)
		{
			return chooser.getSelectedFile().getPath();
		}
		else
		{
			return null;
		}
	}
	
	private boolean saveProject()
	{
		try
		{
			Main.getProject().save();
			return true;
		}
		catch(java.io.IOException exception)
		{
			JOptionPane.showMessageDialog( this, exception.getMessage(), "save failed", JOptionPane.ERROR_MESSAGE );
		}
		return false;
	}
	
	private void newProject(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newProject
	{//GEN-HEADEREND:event_newProject
		final int choice = JOptionPane.showConfirmDialog( this, "Project is modified. Would you like to save it first?", "Question", JOptionPane.YES_NO_CANCEL_OPTION );
		switch(choice)
		{
			case JOptionPane.YES_OPTION:
				if(!saveProject()) return;
				break;
			case JOptionPane.CANCEL_OPTION:
				return;
		}
		Main.newProject();
	}//GEN-LAST:event_newProject
	
	private void quit(java.awt.event.ActionEvent evt)//GEN-FIRST:event_quit
	{//GEN-HEADEREND:event_quit
		if( JOptionPane.showConfirmDialog( this, "Are you sure?", "Confirm quit", JOptionPane.YES_NO_OPTION ) == JOptionPane.NO_OPTION )
		{
			return;
		}
		System.exit(0);
	}//GEN-LAST:event_quit
	
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane developerTab;
    private javax.swing.JTable developerTable;
    private projectprophet.app.DeveloperTaskWidget developerTaskWidget1;
    private javax.swing.JScrollPane developersTaskViewTab;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JMenuItem newProjectMenuItem;
    private javax.swing.JMenuItem openProjectMenuItem;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveProjectMenuItem;
    // End of variables declaration//GEN-END:variables
	
}
